class Storage {
  /** 获取某个值 */
  get(key) {
    return uni.getStorageSync(key)
  }

  /** 设置某个值 */
  set(key, value) {
    uni.setStorageSync(key, value)
  }

  /** 清除某个值 */
  remove(key) {
    uni.removeStorageSync(key)
  }

  /** 清空 */
  clear() {
    uni.clearStorageSync()
  }
}

const storage = new Storage()

/** 用于缓存设备信息 */
const ZE_DEVICE_INFO = 'ze_device_info'
/** Access Token */
const ZE_ACCESS_TOKEN = 'ze_access_token'
/** Refresh Token */
const ZE_REFRESH_TOKEN = 'ze_refresh_token'
/** User Type 1-员工 0-游客 */
const ZE_USER_TYPE = 'ze_user_type'

/** 用户 id */
const ZE_USER_ID = 'ze_user_id'
/** 用户昵称 */
const ZE_NICKNAME = 'ze_nickname'
/** 用户头像 */
const ZE_AVATAR = 'ze_avatar'
/** 用户手机号码 */
const ZE_MOBILE = 'ze_mobile'
/** 办公菜单 */
const ZE_MENUS = 'ze_menus'
// 用户信息
const ZE_USE_INFO = 'ze_user_info'

export function getDeviceInfo() {
  return storage.get(ZE_DEVICE_INFO)
}
export function setDeviceInfo(value) {
  return storage.set(ZE_DEVICE_INFO, value)
}
export function getUseInfo() {
  return storage.get(ZE_USE_INFO)
}
export function setUseInfo(value) {
  return storage.set(ZE_USE_INFO, value)
}

export function getUserType() {
  return storage.get(ZE_USER_TYPE)
}
export function setUserType(value) {
  return storage.set(ZE_USER_TYPE, value)
}

export function getAccessToken() {
  return storage.get(ZE_ACCESS_TOKEN)
}
export function setAccessToken(value) {
  return storage.set(ZE_ACCESS_TOKEN, value)
}

export function getRefreshToken() {
  return storage.get(ZE_REFRESH_TOKEN)
}
export function setRefreshToken(value) {
  return storage.set(ZE_REFRESH_TOKEN, value)
}

export function getNickname() {
  return storage.get(ZE_NICKNAME)
}
export function setNickname(value) {
  return storage.set(ZE_NICKNAME, value)
}

export function getUserId() {
  return storage.get(ZE_USER_ID)
}
export function setUserId(value) {
  return storage.set(ZE_USER_ID, value)
}

export function getAvatar() {
  return storage.get(ZE_AVATAR)
}
export function setAvatar(value) {
  return storage.set(ZE_AVATAR, value)
}

export function getMobile() {
  return storage.get(ZE_MOBILE)
}
export function setMobile(value) {
  return storage.set(ZE_MOBILE, value)
}

export function getMenus() {
  return storage.get(ZE_MENUS)
}
export function setMenus(value) {
  return storage.set(ZE_MENUS, value)
}

/** 清空 token，重定向到登录页 */
export function clearAuth() {
  storage.remove(ZE_ACCESS_TOKEN)
  storage.remove(ZE_REFRESH_TOKEN)
  storage.remove(ZE_USER_TYPE)
  storage.remove(ZE_USER_ID)
  storage.remove(ZE_MENUS)
  const pages = getCurrentPages()
  const from = encodeURIComponent(pages[pages.length - 1].$page.fullPath)
  uni.redirectTo({ url: `/pages/welcome/index?from=${from}` })
}
